<template>
  <view class="login_container">
    <uni-icons type="contact-filled" size="100" color="white"></uni-icons>
    <button :loading="loading" type="primary" class="btn_login" open-type="getPhoneNumber"
      @getphonenumber="onGetPhoneNumber">一键快捷登录</button>
    <view class="tips_text">
      <checkbox style="margin-right: 5px;"></checkbox>登录即同意<text class="link_grement">用户协议</text>和<text
        class="link_grement">隐私协议</text>
    </view>
  </view>
</template>

<script>
  import {
    loginToBackkend
  } from "../../api/user.js"
  export default {
    name: "myLogin",
    data() {
      return {
        loading: false,
        code: '',
      }
    },
    created() {
      // 页面加载时获取微信登录code
      this.getWxLoginCode();
    },
    // 获取微信登录code
    methods: {
      getWxLoginCode() {
        uni.login({
          provider: "weixin",
          success: (res) => {
            this.code = res.code
            // console.log("获取微信登录code：", res.code);
          },
          fail: (err) => {
            console.error("获取微信登录code失败：", err);
            uni.showToast({
              title: "微信登陆失败",
              icon: "none"
            })
          }
        })
      },
      async onGetPhoneNumber(e) {
        // console.log(e)
        if (e.detail.errMsg === "getPhoneNumber:ok") {
          // 用户同意授权
          this.loading = true;
          // const encryptyedData = e.detail.encryptyedData;
          // const iv = e.detail.iv
          const code = e.detail.code
          // console.log("获取手机号加密数据：", encryptyedData, iv);
          // 调用后端登录接口
          if (!code) {
            uni.showToast({
              title: "获取手机号失败：code为空",
              icon: "none"
            });
            this.loading = false;
            return
          }
          const data = await loginToBackkend({
            code: this.code,
            phoneCode: code
          })
          if (data.code === 200) {
            this.loading = false;
            uni.switchTab({
              url: "/pages/home/home"
            });
          }
        } else {
          //用户拒绝授权
          uni.showToast({
            title: "你拒绝了手机号授权",
            icon: "none"
          })
        }
      },
      // loginToBackkend(usercode, code) {
      //   uni.request({
      //     url: `${Base_URL}/api/auth/login`,
      //     method: "POST",
      //     header: {
      //       "Content-Type": "application/json"
      //     },
      //     data: {
      //       code: usercode,
      //       phoneCode: code
      //       // encryptyedData: usercode,
      //       // iv: iv
      //     },
      //     success: (res) => {

      //       this.loading = false;
      //       if (res.statusCode === 200 && res.data.code === 200) {
      //         // 登录成功
      //         const userInfo = res.data.data;
      //         // 保存用户信息到本地存储
      //         uni.setStorageSync("userInfo", userInfo);
      //         uni.setStorageSync("token", userInfo.openid);
      //         // 跳转到首页或其他页面
      //         uni.switchTab({
      //           url: "/pages/home/home"
      //         });
      //       } else {
      //         // 登陆失败
      //         uni.showToast({
      //           title: res.data.message || "登陆失败",
      //           icon: "none"
      //         })
      //       }
      //     },
      //     fail: (err) => {
      //       this.loading = false;
      //       console.error("登录请求失败：", err);
      //       uni.showToast({
      //         title: "网络请求失败",
      //         icon: "none"
      //       })
      //     }
      //   })
      // }
    }

  }
</script>

<style lang="scss">
  .login_container {
    height: 750rpx;
    // background-color: #f8f8f8;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;

    .btn_login {
      width: 90%;
      border-radius: 100px;
      margin: 15px 0;
      background-color: blue;


    }

    .tips_text {
      font-size: 16px;
      color: gray;
    }

    .link_grement {
      color: blue;
    }
  }
</style>